/**
 * @copyright Copyright (c) 2023..
 *
 * @file    file_task.h
 * @brief   描述 文件上传任务
 * @author  HC(202206728@any3.com)
 * @version 1.0
 * @date    2023-09-27
 */
#ifndef FILE_TASK_H
#define FILE_TASK_H

#include <string>
#include <mutex>

#include "common_opr_all.h"
#include "eventcommand.h"
#include "eventloop.h"
namespace DcAdr
{
    // 异步合并所需文件信息
    typedef struct MergeParam
    {
        string chunkFileMd5;
        string fileMd5;
        string fileHandle;
        string fileName;
        string filePath;
    }MergeParam; 
    
    class FileTask: public EventCommand
    {
    public:
        FileTask();
        ~FileTask();
        void upload(const FileUploadParam &fileup);
        void s3_upload(const FileUploadParam &fileup);
        bool getUploadLink(const std::string &filePath, const std::string &fileName, const std::string &extras);
        void uploadLinkAnalysis(const std::string Jsondata);

        bool doUploadFile();
        bool uploadFileAnalysis(const std::string &Jsondata);

        bool doUploadChunkFile();
        bool uploadChunkFileAnalysis(const std::string &Jsondata);

        bool uploadResult();
        bool uploadResultAnalysis(const std::string &Jsondata);

        // 文件上传状态函数
        void fileUploadStatus(const FileUploadParam &info, const uint32_t &fileuploadstatus);

    private:
        void setFileUploadParam(const FileUploadParam &info);
        void speedStatistics(const std::string &filePath, int state, int result);
        void removeNoFgDat(const std::string &filePath);
        // 判断车端C2证书是否存在
        bool certificateExistJudge();

        // 分片已合并,查询合并结果
        bool chunkMergedCheckResult(Json::Value &oneFile);
        // 分片文件合并结果接口
        bool checkMergeStatus(const string &fileHandle);
        bool mergeStatusAnalysis(const string &filePath, const string &fileHandle);

        // 上传剩余分片
        bool uploadRemainChunk(const int curChunkTime, const int totalChunkTime);
        // 分片文件分割
        bool chunkFileSplit(const int curChunkTime, std::string &chunkFilePath, std::string &chunkFileMd5);

        // 调用合并接口
        void callMergeInterface();
        bool mergeParamAnalysis(Json::Value &oneFile, MergeParam &mergeParam);
        // 分片文件合并
        bool chunkFileMerge(MergeParam &mergeParam);
        
        std::string m_headerName;
        std::string m_headerValue;
        std::string m_uploadLinkMsg;
        // 文件handle
        std::string m_uploadTaskIdMsg;
        std::string m_fcToken;
        std::string m_uploadFileResultMsg;

        CommonOprHttp comoprhttp;

        mutex mutex_writeReuploadFile;

        // 文件上传参数
        FileUploadParam fileParam;

        // 分片合并中
        bool isMerging = false;

        // 文件上传超时字符串
        const std::string timeoutStr = "Timeout was reached";
    };
}

#endif // FILE_TASK_H